package com.helloworld.chat.dao.impl;

import com.helloworld.chat.dao.GroupDao;
import com.helloworld.chat.entity.Group;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by xdzy on 2017/6/28.
 */
@Repository
public class GroupDaoImpl  implements GroupDao{
    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    @Override
    public void addGroup(Group group) {
        sessionFactory.getCurrentSession().save(group);
    }

    @Override
    public Group getGroup(int gid) {
        return sessionFactory.getCurrentSession().get(Group.class,gid);
    }

    @Override
    public List<Group> getGroupList(int uid) {
        String hql="from Group as g where g.uid=:uid";
        Query<Group> query=sessionFactory.getCurrentSession().createQuery(hql,Group.class);
        query.setParameter("uid",uid);
        return query.list();
    }
}
